fix: Enforce strict JSON Schema compliance to prevent OpenAI 400 Bad Request errors#1266
fix: Enforce strict JSON Schema compliance to prevent OpenAI 400 Bad Request errors#1266svetanis wants to merge 1 commit into
Conversation
|
Hi @svetanis, thank you for your contribution! We appreciate you taking the time to submit this pull request. I noticed that your changes are not covered by the current test cases and the test you included passes even without your changes. Could you please include the corresponding unit tests to verify your changes? |
1f64496 to
7017e53
Compare
|
Hello @hemasekhar-p , Thank you for the review and the feedback! I've updated the PR and added three new dedicated unit tests in
I have also successfully rebased the branch on top of the latest Additionally, I ran comprehensive end-to-end testing on the rebased branch, and all of the demos (including a new one validating the Please let me know if there is anything else you need! |
|
@svetanis, thank you for addressing the comments. Currently this PR is under review by our team, we will keep you posted if any additional information is required. thank you. |
|
@anFatum, Could you please review this PR. |
Enforce strict JSON Schema compliance to prevent OpenAI 400 Bad Request errors
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
When using strict OpenAI-compatible providers like Groq, empty function arguments or responses are serialized as
nullor omitted byChatCompletionsRequest. Furthermore, zero-argument tools fail to declare aparametersschema object. This breaks strict JSON schema parsers, causing them to drop conversation history, which leads to400 Bad Requestexceptions due to models hallucinating raw XML<function>tags. JSON Schema enum types are also improperly serialized in uppercase.Solution:
schemaNormalizerModuleto theObjectMapperinChatCompletionsRequest.javato forceTypeenums to serialize in lowercase (e.g.,"string").enforceJsonObjecttoChatCompletionsCommon.javato guarantee that emptyargumentsand functionresponsepayloads fallback to"{}"instead ofnullor raw strings.ChatCompletionsRequest.javato explicitly inject a{"type":"object", "properties":{}}parameters schema for zero-argument functions instead of omitting it.Testing Plan
Unit Tests:
Passed:
mvn testAdded
testFromLlmRequest_withEmptyFunctionArgumentstoChatCompletionsRequestTest.javato explicitly test"{}"serialization for zero-argument tools. UpdatedtestFromLlmRequest_withFunctionResponseexpectations.Manual End-to-End (E2E) Tests:
Wired the Google native chat completion client into the external
model-prismproject (PR #1199). Ran the integration through all the demo tests covering all main ADK features. The applications successfully execute multi-turn multi-tool conversations using strict JSON schema endpoints without throwing400 Bad Requestdue to context loss.Checklist
Additional context
This change unlocks robust support for the rapidly growing ecosystem of OpenAI-compatible endpoints that enforce strict JSON Schema validation.